package com.beamfield.controller.manage.mixstation;

import com.alibaba.fastjson.JSON;
import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.model.MixCar;
import com.beamfield.model.MixCarMaintain;
import com.beamfield.service.manage.mixstation.MixCarService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author chenjj
 * @Date 2018/9/25
 * 车队管理
 */
@Controller
@RequestMapping("/cms/mixstation/car")
public class MixCarController extends BaseController{

    @Autowired
    private MixCarService mixCarService;

    @Value("${session.beam}")
    private String beam;
    @Value("${session.user}")
    private String user;

    @RequestMapping(value = "/carList")
    public ModelAndView carList(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/editCar")
    public ModelAndView editCar(){
        return new ModelAndView();
    }

    /**
     * 获取车辆列表
     * @param offset
     * @param limit
     * @param plateNumber
     * @param leasingCompany
     * @return
     */
    @RequestMapping("/getCarList")
    @ResponseBody
    public PageResult getCarList(Integer offset, Integer limit, String plateNumber, String leasingCompany, HttpSession session){
        Long beamId = (Long) session.getAttribute(beam);
        return mixCarService.getCarList( offset,limit,plateNumber,leasingCompany,beamId);
    }

    /**
     * 新增or编辑车辆
     *
     * @param mixCar
     * @return
     */
    @RequestMapping("/doEditCar")
    @ResponseBody
    public ApiResult doEditCar(MixCar mixCar, HttpSession session) {
        Long userId = (Long) session.getAttribute(user);
        Long beamId = (Long) session.getAttribute(beam);

        return mixCarService.insertOrUpdateCar(mixCar,userId,beamId);
    }

    /**
     * 删除车辆
     * @return
     */
    @RequestMapping("/delCar")
    @ResponseBody
    public ApiResult delCar(String id){
        if(StringUtils.isNotBlank(id)) {
            return mixCarService.delCar(id);
        }
        else {
            return new ApiResult("500","未获取到该车辆");
        }
    }

    /**
     *获取车辆信息
     *@return
     */
    @RequestMapping("/getCarInfo")
    @ResponseBody
    public ApiResult getCarInfo(Long id){
        return mixCarService.getCarInfo(id);
    }

    /**
     * 导出车辆
     *
     * @param response
     * @param offset
     * @param limit
     * @param plateNumber
     * @param leasingCompany
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping("/exportCarList")
    @ResponseBody
    public String exportCarList(HttpServletResponse response, Integer offset, Integer limit, String plateNumber, String leasingCompany, HttpSession session) throws Exception {
        Long beamId = (Long) session.getAttribute(beam);
        return mixCarService.exportCarList(response,offset,limit,plateNumber,leasingCompany,beamId);
    }

    @RequestMapping(value = "/carMaintainList")
    public ModelAndView carMaintainList(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/editCarMaintain")
    public ModelAndView editCarMaintain(){
        return new ModelAndView();
    }

    /**
     * 获取车辆维修保养列表
     * @param offset
     * @param limit
     * @param carNo
     * @param maintainType
     * @param startDate
     * @return
     */
    @RequestMapping("/getCarMaintainList")
    @ResponseBody
    public PageResult getCarMaintainList(Integer offset, Integer limit, String carNo, String maintainType, String startDate, HttpSession session){
        Long beamId = (Long) session.getAttribute(beam);
        return mixCarService.getCarMaintainList( offset,limit,carNo,maintainType,startDate,beamId);
    }

    /**
     * 新增车辆维修保养
     *
     * @param maintainJson
     * @return
     */
    @RequestMapping("/doEditCarMaintain")
    @ResponseBody
    public ApiResult doEditCarMaintain(String maintainJson, HttpSession session) {
        Long userId = (Long) session.getAttribute(user);
        Long beamId = (Long) session.getAttribute(beam);

        MixCarMaintain mixCarMaintain = JSON.parseObject(maintainJson,MixCarMaintain.class);
        return mixCarService.insertCarMaintain(mixCarMaintain,userId,beamId);
    }
}
